package com.yisu.expressway.manager.website.controller;


import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.Employee;
import com.yisu.expressway.manager.core.service.UserService;
import com.yisu.expressway.manager.domain.request.LoginRequest;
import com.yisu.expressway.manager.domain.request.ModifyPasswordRequest;
import com.yisu.expressway.manager.domain.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.*;

/**
 * Created by LKL on 2016/9/12.
 */
@RequestMapping(value = "/user")
@Controller
public class UserController extends BaseController{

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login.json")
    @ResponseBody
    public JsonResult login(HttpServletRequest request, LoginRequest loginRequest){
        if(StringUtils.isEmpty(loginRequest.getEmployeeNumber())
                || StringUtils.isEmpty(loginRequest.getPassword())) {
            logger.error("Employee number and name can not empty!");
            return new JsonResult(PARAMETER_ERROR.code, PARAMETER_ERROR.msg);
        }

        Employee employee = new Employee();
        employee.setEmployeeNumber("0001");
        employee.setEmployeeName("超级管理员");
        if(employee != null) {
            logger.info("User[{}] logged in successfully.", loginRequest.getEmployeeNumber());
            request.getSession().setAttribute(Constants.USER_INFO_KEY, employee);
            return new JsonResult();
        } else {
            logger.error("User[{}] failed to login. cause: [{}].", loginRequest.getEmployeeNumber(), CUSTOMER_NOT_EXIST.msg);
            return new JsonResult(CUSTOMER_NOT_EXIST.code, CUSTOMER_NOT_EXIST.msg);
        }

    }

    @RequestMapping(value = "/logout.json")
    @ResponseBody
    public JsonResult logout(HttpServletRequest request){
        Employee employee = (Employee) request.getSession().getAttribute(Constants.USER_INFO_KEY);
        request.getSession().removeAttribute(Constants.USER_INFO_KEY);

        logger.info("User[{}] logged out.", employee.getEmployeeName());
        return new JsonResult();
    }

    @RequestMapping(value = "/modifyPassword.json")
    @ResponseBody
    public JsonResult modifyPassword(HttpServletRequest request, ModifyPasswordRequest modifyPasswordRequest){
        if(StringUtils.isEmpty(modifyPasswordRequest.getNewPass()) ||
                !modifyPasswordRequest.getNewPass().equals(modifyPasswordRequest.getRePass())) {
            logger.error("Two passwords is inconsistent.");
            return new JsonResult(PASSWORD_INCONSISTENT.code, PASSWORD_INCONSISTENT.msg);
        }

        Employee employee = (Employee) request.getSession().getAttribute(Constants.USER_INFO_KEY);
        modifyPasswordRequest.setEmployeeId(employee.getId());
        modifyPasswordRequest.setEmployeeNumber(employee.getEmployeeNumber());
        if(userService.modifyPassword(modifyPasswordRequest) > 0) {
            logger.info("User[{}] modified password successfully.", employee.getEmployeeName());
            return new JsonResult();
        } else {
            logger.info("User[{}] failed to modify password. cause: [{}]", employee.getEmployeeName(), FAILURE.msg);
            return new JsonResult(FAILURE.code, FAILURE.msg);
        }
    }


    @RequestMapping(value = "/indexPage.htm")
    public String indexPage(){
        return "/main/index";
    }
    @RequestMapping(value = "/welcome.htm")
    public String welcome(){
        return "/main/welcome";
    }

}
